// pages/visitor_submit_List/index.js
const appInstance = getApp()
import drawQrcode from 'weapp-qrcode'
Component({
  // options:{
  //   multipleSlots:true
  // },
  data: {
    activeValuess: [0],
    enable: false,
    scrollTop: 0,
    overlay_visible:false,
    loading_visible:false,
    page_start:1,
    show_reject_dialog:false,
    activeValues: [],
    cur: {},
    position: [
      { value: 'top', text: '顶部弹出' },
      { value: 'left', text: '左侧弹出' },
      { value: 'center', text: '中间弹出' },
      { value: 'bottom', text: '底部弹出' },
      { value: 'right', text: '右侧弹出' },
    ],
    htmlSnip:`<div>
    <h4> fa123465
    <span style="display: inline-block;margin-left: 15px;width:80px;border-radius:10px;text-align:center;font-size: 15px;color:	#FFFFFF;background-color:#00BFFF">Approved</span>
    </h4>
  </div>
  `
  },
  methods: {
    return_visitor_submit_list_page(){
      this.setData({
        todo_content_detail_popup:false
      })
    },
    onVisibleChange(e) {
      this.setData({
        todo_content_detail_popup:false
      })},
    popup_todo_content_detail(e){
      let that = this
      let current_id = e.currentTarget.dataset.id
      that.data.visitor_submit_list.forEach(function(item, index) {
      if (item.id == current_id){
        that.setData({
        todo_content_detail_popup:true,
        todo_content_detail_obj:item
      })}
      });
    },
    onRefresh() {
      this.setData({ enable: true });
      this.onShow()
      setTimeout(() => {
        this.setData({ enable: false });
      }, 1500);
  },
    onScroll(e) {
      const { scrollTop } = e.detail;
      this.setData({ scrollTop });
    },
    input_reject_reason(e){
      this.setData({approved_reject_reason:e.detail.value})
    },
    showRejectDialog(e) {
      console.log(e)
      let order_id = e.currentTarget.dataset.id
      let approval_tag = e.currentTarget.dataset.value
      this.setData({order_id:order_id,approval_tag:approval_tag,show_reject_dialog:true})
    },
    showConfirmDialog(e) {
      console.log(e)
      let order_id = e.currentTarget.dataset.id
      let approval_tag = e.currentTarget.dataset.value
      this.setData({order_id:order_id,approval_tag:approval_tag,show_confirm_dialog:true})
    },
    closeDialog() {
      this.setData({ show_reject_dialog: false, show_confirm_dialog:false});
    },
    closeOverlay() {
      this.setData({overlay_visible:false, loading_visible:false})
    },
    last_page:function (e) {
      let page_last_start = this.data.page_start -1
      if (page_last_start <= 0){
        this.setData({page_start:1})
        this.onShow()
      }else{
        this.setData({page_start:page_last_start})
        this.onShow()
      }
    },
    next_page:function (e) {
      let page_next_start = this.data.page_start + 1
      if (page_next_start > this.data.page_total_number){
        this.setData({page_start:1})
        this.onShow()
      }else{
        this.setData({page_start:page_next_start})
        this.onShow()
      }
    },
    handleChange(e) {
      this.setData({
        activeValues: e.detail.value,
      });
    },
    onShow() {
      let that = this
      appInstance.setLoginData(this)
      console.log(appInstance.globalData)
      wx.request({
        url: appInstance.globalData.request_url + "factory/approval_list",
        header: {
          'Cookie': appInstance.globalData.cookies
        },
        data:{
          'page_start':this.data.page_start,
        },
        method:"POST",
        dataType:"json",
        success(resp) {
          if (resp.statusCode === 500) {
            console.log(resp)
            // wx.showToast({
            //   title: resp.data,
            //   icon: 'error',
            //   duration: 2000
            // })
          } else if (appInstance.globalData.user_status == 'has_login') {
            console.log(resp)
            console.log(resp)
            that.setData({
              visitor_submit_list:resp.data.result.visitor_submit_list,
              activeValues : resp.data.result.visitor_activeValues,
              page_total_number:resp.data.result.page_total_number
            })
          }
        },
        fail(err) {
          wx.showToast({
            title: err,
            icon: 'error',
            duration: 2000
          })
        }
      })

    },

    approver_button:function(e){
      this.closeDialog() 
      this.setData({overlay_visible:true, loading_visible:true})
      let order_id = this.data.order_id
      let approval_tag = this.data.approval_tag
      let that = this
      wx.login({
        success (res) {
          if (res.code) {
            wx.request({
              url: appInstance.globalData.request_url + "factory/approval",
              data:{
                "order_id":order_id,
                "state":approval_tag,
                'code':res.code,
                'approved_reject_reason':that.data.approved_reject_reason
              },
              header: {
                'Cookie': appInstance.globalData.cookies
              },
              method:"POST",
              dataType:"json",
              success(resp) {
                that.closeOverlay()
                if (resp.statusCode === 500) {
                  console.log(500)
                  wx.showToast({
                    title: resp.data,
                    icon: 'error',
                    duration: 2000
                  })
                  that.closeDialog() 
                } else {
                  console.log(200)
                  if(resp.data.error){
                    wx.showToast({
                      title: resp.data.error.data.message,
                      icon: 'error',
                      duration: 2000
                    })
           
                  }else{
                    wx.showToast({
                      title: "操作成功",
                      icon: 'success',
                      duration: 2000
                    })   
                    that.onShow()
                  }
                }
              },
              fail(err) {
                console.log(err)
                that.closeOverlay()
                wx.showToast({
                  title: err,
                  icon: 'error',
                  duration: 2000
                })
              }
            
            })
          } else {
            that.closeOverlay()
            console.log('登录失败！' + res.errMsg)
          }
        }
      })

    },
  
}
})




